/*
 * Copyright (c) 2019 - 2020 IoT Company of Midea Group.
 *
 * File Name 	    : 
 * Description	    : rtos adaptor
 *
 * Version	        : v0.1
 * Author			: 
 * Date	            : 2019/09/15  refactoring
 * History	        : 
 */
#ifndef __MS_OSAL_TIMER_H__
#define __MS_OSAL_TIMER_H__

#include <stdint.h>
#include <stdbool.h>
#include "ms_osal.h"

#ifdef __cplusplus
extern "C"
{
#endif

#define MS_OSAL_WAIT_FOREVER ((uint64_t)-1)

typedef enum
{
	/* The timer will be a one-shot and enter the dormant state after it expires */
	MS_OSAL_TIMER_ONE_SHOT,
	/* The timer will expire repeatedly with a frequency set by the TimerPeriod(ticks) parameter */
	MS_OSAL_TIMER_PERIODIC,
} ms_osal_timer_mode_t;

typedef enum
{
	/* Start the timer on creation */
	MS_OSAL_TIMER_AUTO_ACTIVATE,
	/* Do not start the timer on creation */
	MS_OSAL_TIMER_NO_ACTIVATE,
} ms_osal_timer_activate_t;

/**
 * @brief Create a new software timer instance
 * @param[in]	timer_t:	A handle to the newly created timer, NULL when error
 * @param[in]	name:	A text name that is assigned to the timer
 * @param[in]	ticks:	The timer period that is defined in tick period
 * @param[in]	call_back: The function to call when the timer expires
 * @param[in]	cb_arg: 	An identifier that is assigned to the timer being created
 * @param[in]	reload: Define whether to reload timer when expires
 * @param[in]	activate:Define whether to start timer when it is created
 * @return if successly, return MS_OSAL_RESULT_SUCCESS, \n
 *         otherwise return MS_OSAL_RESULT_ERROR
 **/
ms_osal_result_t ms_osal_timer_create(ms_osal_timer_t *timer,
										const char *name,
										ms_osal_timer_tick_t ticks,
										void (*call_back)(void *),
										void *cb_arg,
										ms_osal_timer_mode_t reload,
										ms_osal_timer_activate_t activate);

/**
 * @brief Start a timer that was previously created
 * @param[in]	timer_t: A handle to the newly created timer
 * @return if successly, return MS_OSAL_RESULT_SUCCESS, \n
 *         otherwise return MS_OSAL_RESULT_ERROR
 **/
ms_osal_result_t ms_osal_timer_activate(ms_osal_timer_t *timer);

/**
 * @brief Delete a timer that was previously created
 * @param[in]	timer_t: A handle to the newly created timer
 * @return if successly, return MS_OSAL_RESULT_SUCCESS, \n
 *         otherwise return MS_OSAL_RESULT_ERROR
 **/
ms_osal_result_t ms_osal_timer_delete(ms_osal_timer_t *timer);

/**
 * @brief	delay ms
 * @param[in]	msecs: millisecond
 */
void ms_osal_timer_delay_ms(int msecs);

/**
 * @brief	get SysTime millisecond
 * @return	millisecond
 */
unsigned int ms_osal_timer_get_systime_ms(void);

/**
 * @brief	Get the current OS ticks that will never overflow
 * @return	The current OS ticks
 **/
uint64_t ms_osal_ticks_get(void);

/**
* @brief	Convert millisecond to the number of OS ticks
* @param[in]	msecs: millisecond
* @return	the number of OS ticks
**/
uint32_t ms_osal_msec_to_ticks(uint32_t msecs);

/**
 * @brief	Convert the number of OS ticks to millisecond
 * @param[in]	ticks: the number of OS ticks
 * @return	millisecond
 **/
uint32_t ms_osal_ticks_to_msec(uint32_t ticks);

/**
 * @brief	compare start tick and end tick, whether more than interval tick
 * @param[in] end_tick : latest tick
 * @param[in] start_tick : presave tick
 * @param[in] interval_tick : interval tick
 * @return if less than interval tick, return false, \n
 * 		   otherwise return true
 **/
bool ms_osal_ticks_compare_result(uint32_t end_tick, uint32_t start_tick, uint32_t interval_tick);

#ifdef __cplusplus
}
#endif

#endif // __MS_OSAL_TIMER_H__
